package com.dzpk.gameserver.util;

import com.dzpk.gameserver.module.player.Player;
import org.apache.mina.core.session.IoSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import somnus.net.action.Action;

public class CommandExecuteAop {

    private static final Logger log = LoggerFactory.getLogger("command_execute_listener");

    private long bestTime;

    public void setBestTime(long bestTime) {
        this.bestTime = bestTime;
    }

    public Object aopAround(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.nanoTime();
        Action cmd = (Action) pjp.getTarget();
        Object[] args = pjp.getArgs();
        long sessionId = 0;
        long roleId = 0;
        String roleName = null;
        if (args.length > 0) {
            IoSession s = (IoSession) (args[0]);
            Player p = GameServerUtils.getSessionAttr(s, GameServerConstants.SIGN_SESSION_PLAYER);
            try {
            } catch (Exception e) {
            }
        }
        Object result = pjp.proceed();

        long endTime = System.nanoTime();

        double cost = (endTime - startTime) * 0.000001;

        if (cost > bestTime) {
            log.info(String.format("exec execute %s(%s) cost %sms sessionId(%s) roleId(%s) roleName[%s]", cmd.getDesc(), cmd
                    .getMessageId(), cost, sessionId, roleId, roleName));
        }

        return result;
    }

}
